from game import zz_game
from zz import zz_log, zz_str
import time


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "鸡场"

    def game_alert(self):
        self.alert_close_by_file("./game/img/淘宝农场_鸡场")

    def home(self):
        """
        家庭签到
        """
        self.log_msg("点击家庭")
        self.img_click('./game/img/淘宝农场_鸡场/home_index.png')
        self.log_msg("等待家庭主页")
        self.img_exit('./game/img/淘宝农场_鸡场/home_main.png', 30)
        self.log_msg("已进入家庭主页")
        self.game_alert()

        if not self.img_exit('./game/img/淘宝农场_鸡场/home_qian.png', 2):
            self.log_msg("已签到")
        else:
            self.log_msg("进行签到")
            self.img_click('./game/img/淘宝农场_鸡场/home_qian.png')

        self.log_msg("点击返回主界面")
        self.d.sleep(2)
        self.d.press("back")
        self.d.sleep(2)
        while not self.img_exit('./game/img/淘宝农场_鸡场/egg_index.png', 2):
            self.log_msg("返回主界面失败-再返回")
            self.d.press("back")
            self.d.sleep(2)

    def task(self):
        """
        任务奖励领取
        """
        self.log_msg("点击领饲料")
        self.img_click('./game/img/淘宝农场_鸡场/task_index.png')
        self.log_msg("等待打开任务列表")
        self.img_exit('./game/img/淘宝农场_鸡场/task_main.png', 30)
        self.log_msg("已打开任务列表")

        self.log_msg("开始领任务奖励")
        try_index = 0
        while True:
            if self.img_exit('./game/img/淘宝农场_鸡场/task_full.png', 1):
                self.log_msg("饲料已满，退出")
                self.img_click("./game/img/淘宝农场_鸡场/task_full_1.png", 3)
                self.img_click("./game/img/淘宝农场_鸡场/task_full_2.png", 3)
                break

            # if not self.img_exit("./game/img/淘宝农场_鸡场/task_get.png", 2, 0.94):
            prize_item = None
            items = self.d.xpath('//android.widget.TextView').all()
            for item in items:
                if item.text.startswith('x') and item.text.endswith("g领取"):
                    prize_item = item
                    break
                else:
                    continue
            if prize_item is None:
                if try_index > 5:
                    self.log_msg("已完成领取奖励")
                    break
                self.swipe_up()
                try_index += 1
            else:
                self.log_msg("点击领取")
                prize_item.click()
                # self.img_click("./game/img/淘宝农场_鸡场/task_get.png")

        self.img_click_if_exists("点击关闭任务列表", './game/img/淘宝农场_鸡场/task_close.png', './game/img/淘宝农场_鸡场/task_index.png')

    def egg(self):
        """
        捐蛋
        """
        self.img_click_if_exists("点击捐蛋列表", './game/img/淘宝农场_鸡场/egg_index.png', './game/img/淘宝农场_鸡场/egg_list.png')
        self.img_click_if_exists("点击立即捐蛋", './game/img/淘宝农场_鸡场/egg_list.png', './game/img/淘宝农场_鸡场/egg_01.png')
        self.img_click_if_exists("点击去捐蛋", './game/img/淘宝农场_鸡场/egg_01.png', './game/img/淘宝农场_鸡场/egg_02.png')
        self.log_msg("点击立即捐蛋")
        self.img_click('./game/img/淘宝农场_鸡场/egg_02.png')
        while not self.img_exit('./game/img/淘宝农场_鸡场/egg_done.png'):
            self.log_msg("等待捐蛋完成")
            self.d.sleep(1)
        self.log_msg("捐蛋完成，返回")
        self.d.press("back")
        self.d.press("back")
        self.d.sleep(2)
        self.log_msg("点击关闭蛋列表")
        self.img_click('./game/img/淘宝农场_鸡场/egg_list_close.png')

    def play_task(self, timeout=30):
        self.d.sleep(2)
        self.alert_close_by_file('./game/img/淘宝农场_鸡场', 0.8, 'xtask_')

        self.log_msg(f"已进入浏览页面，开始浏览{timeout}秒")
        deadline = time.time() + timeout
        while True:
            self.swipe_up()
            self.d.sleep(1.5)
            self.swipe_up()
            self.d.sleep(1.5)
            self.swipe_down()
            self.d.sleep(1.5)
            self.swipe_down()
            self.d.sleep(1.5)
            if time.time() > deadline:
                self.log_msg("浏览完成")
                break
        self.d.press("back")

    def chou(self):
        """
        抽
        """
        self.log_msg("点击房子")
        self.d.click(400, 1200)
        self.log_msg("等待进入房子")
        self.img_exit('./game/img/淘宝农场_鸡场/chou_index.png', 30)
        self.log_msg("已进入房子")
        self.img_click_if_exists("点击抽奖", './game/img/淘宝农场_鸡场/chou_index.png', './game/img/淘宝农场_鸡场/chou_prize.png')
        self.swipe_up()

        while True:
            if self.d.xpath('//android.widget.Button[@text="领取"]').wait(1):
                self.log_msg("点击领取")
                self.xpath_click('//android.widget.Button[@text="领取"]')
                self.d.sleep(1)
                continue

            task = self.d.xpath('//android.widget.Button[@text="去完成"]').get()
            child = task.parent().elem.getchildren()
            title = child[0].attrib['text']
            self.log_msg(title)
            if zz_str.contains(title, '逛一逛'):
                self.log_msg("点击进入任务")
                task.click()
                self.play_task(16)
                self.d.sleep(1)
            else:
                break

        self.log_msg("浏览任务已完成，开始抽奖")
        while True:
            if self.d.xpath('//android.widget.TextView[@text="还剩0次机会"]').wait(2):
                self.log_msg("机会已抽完")
                break
            self.log_msg("开始抽奖")
            self.img_click('./game/img/淘宝农场_鸡场/chou_prize.png')
            while True:
                if self.img_exit('./game/img/淘宝农场_鸡场/alert_0_close.png', 2):
                    self.log_msg("点击关闭弹窗")
                    self.img_click('./game/img/淘宝农场_鸡场/alert_0_close.png')
                    break
                self.log_msg("抽奖中")
        self.log_msg("开始抽奖完毕，点击返回")
        self.d.press("back")
        self.d.sleep(1)
        self.d.press("back")

    def handle_main(self):
        if zz_game.config_get('__task_ji') == 1:
            self.log_msg("任务已完成")
            return True

        self.log_msg("点击鸡场")
        # self.d.xpath('//*[@text="芭芭农场"]').click()
        self.img_click("./game/img/淘宝农场_鸡场/game_index.png")

        self.log_msg("等待进入鸡场主界面")
        if not self.img_exit('./game/img/淘宝农场_鸡场/egg_index.png', 30):
            return False
        self.log_msg("已进入鸡场主界面")

        self.game_alert()

        if zz_game.config_get('__task_ji') != 2:
            self.egg()  # 捐蛋
            zz_game.config_set('__task_ji', 2)

        self.home()
        self.chou()
        self.task()

        zz_game.config_set('__task_ji', 1)  # 标记完成

        self.d.sleep(2)
        self.close_game()

        return True
